<template>
  <div class='container'>
     <el-dialog :title=" isEdit?'新增目录':'修改目录'" :visible="dialogFormVisible" @close="close" @open="open" width="400px">
      <el-form :model="form" label-position="left" ref="form" :rules="rules">
          <el-form-item v-if="!$route.query.id" label="所选学科" label-width="80px" prop="subjectID">
          <el-select v-model="form.subjectID" placeholder="请选择" style="width:280px">
            <el-option v-for="item in subjectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" label-width="80px" prop="directoryName">
          <el-input v-model="form.directoryName" placeholder="请输入目录名称" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="addSubject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { add, detail, update } from '@/api/hmmm/directorys.js'
export default {
  name: 'directorys-add',
  data () {
    return {
      form: {
        subjectID: null,
        directoryName: null
      },
      rules: {
        subjectID: [{ required: true, message: '学科不能为空', trigger: 'change' }],
        directoryName: [{ required: true, message: '目录不能为空', trigger: 'blur' }]
      },
      subjectList: []
    }
  },
  props: {
    dialogFormVisible: {
      type: Boolean
    },
    isEdit: {
      type: Boolean
    },
    isEditId: {
      type: Number
    }
  },
  created () {
    this.getSubjectData()
  },
  methods: {
    // 学科简单列表
    async getSubjectData () {
      const { data } = await simple()
      this.subjectList = data
    },
    // 弹窗打开回调
    async open () {
      // 如果是编辑
      if (this.isEdit) {
        const { data } = await detail({ id: this.isEditId })
        this.form.subjectID = data.subjectID
        this.form.directoryName = data.directoryName
      }
      // 路由参数
      if (this.$route.query.id) {
        this.form.subjectID = this.$route.query.id
      }
    },
    // 关闭弹窗的回调
    close () {
      this.$emit('update:dialogFormVisible', false)
      this.$refs.form.resetFields()
    },
    // 确认
    addSubject () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            const data = await update({
              subjectID: this.form.subjectID,
              directoryName: this.form.directoryName,
              id: this.isEditId
            })
            console.log(data)
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            await add(this.form)
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }
          this.$emit('dataUpdate')
          this.close()
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
</style>
